package com.admin.scroller.photoorvideo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.admin.scroller.photoorvideo.callback.OnSavePhoto;
import com.admin.scroller.photoorvideo.tasks.BitmapSaveTask;
import com.admin.scroller.photoorvideo.view.CropImageView;

/**
 * 照片剪裁页面
 */
public class CropActivity extends AppCompatActivity implements View.OnClickListener {

    private CropImageView cropImageView;
    private ImageView ivBack;
    private TextView tvCrop;
    private String url;
    private Bitmap source;

    private OnSavePhoto savePhoto=new OnSavePhoto() {
        @Override
        public void onSuccess(String path) {
            Intent intent=new Intent();
            intent.putExtra("path",path);
            setResult(RESULT_OK,intent);
            finish();
        }

        @Override
        public void onError(String error) {
            Toast.makeText(CropActivity.this,"图片剪裁失败！",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);

        cropImageView=findViewById(R.id.crop_view);
        url=getIntent().getStringExtra("url");
        source=BitmapFactory.decodeFile(url);
        cropImageView.setImageBitmap(source);
        cropImageView.setCropMode(CropImageView.CropMode.FREE);

        ivBack=findViewById(R.id.iv_back);
        tvCrop=findViewById(R.id.tv_finish_crop);

        ivBack.setOnClickListener(this);
        tvCrop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.iv_back){
            finish();
        }else if(v.getId()==R.id.tv_finish_crop){
            Bitmap bitmap=cropImageView.getCroppedBitmap();
            BitmapSaveTask task=new BitmapSaveTask(savePhoto);
            task.execute(bitmap);
        }
    }
}
